<cfsetting enablecfoutputonly="Yes">

<!--- 
  /**
   * This file will handle all the statuses within the application. All statuses
   * will be 'cached' within the Application scope and will be copied to the 
   * request scope if needed. As soon as a variable called 'nStatus' is defined
   * within the attributes scope, a status will be copied from the Application
   * scope
   *
   * @author  <info@mireau.net>
   * @version 1.0 - <yyyy/mm/dd>
   */
--->

<cfscript>
  /* Define constants for error handling */
  function index()
  {
    if( not isDefined( "nColumnIndex" ))
    {
      nColumnIndex = 0;
    }
    
    nColumnIndex = nColumnIndex + 1;
    
    return( nColumnIndex );
  }

  request.status.n_ONLY_INDEXES_PERMITTED = index();
  request.status.n_ADDED_SYSTEM_FUSE      = index();
  request.status.n_INVALID_NEW_DIR_GIVEN  = index();
  request.status.n_ERROR_CREATING_DIR     = index();
  request.status.n_CREATED_DIR            = index();
  request.status.n_DIR_ALREADY_EXISTS     = index();
  request.status.n_PLP_NO_NEXT_FUSE       = index();
  request.status.n_LOGIN_FAILED           = index();
  request.status.n_LOGOUT_SUCCESS         = index();
  request.status.n_ACCESS_DENIED          = index();
  request.status.n_INSERT_SECCESS         = index();
  request.status.n_UPDATE_SECCESS         = index();
  request.status.n_DELETE_SECCESS         = index();
</cfscript>

<!--- If no status array existed in the Application scope, make one --->
<cflock
  timeout="5"
  scope="Application"
  type="Exclusive"
>
  <cfscript>
    if( not isDefined( "application.xStatus" ) or isDefined( "attributes.bRecycle" ))
    {
      /*
       * Two dimensional array. The first dimension is the language (by default
       * there will be only ONE language, but it's for FORWARD COMPATIBILITY
       * (a new concept for a change ;)))
       */
      application.xStatus = arrayNew( 2 );

      application.xStatus[1][request.status.n_ONLY_INDEXES_PERMITTED] = "You are you only permitted to do request on an index.cfm file in any Mireau application.";
      application.xStatus[1][request.status.n_ADDED_SYSTEM_FUSE]      = "You have successfully created a new fuse within the application.";
      application.xStatus[1][request.status.n_INVALID_NEW_DIR_GIVEN]  = "The directory (path) that you provided was invalid. As a result, it could not be created.";
      application.xStatus[1][request.status.n_ERROR_CREATING_DIR]     = "An error occured when the system tried to create this directory for you.";
      application.xStatus[1][request.status.n_CREATED_DIR]            = "The directory was successfully created by the Mireau system.";
      application.xStatus[1][request.status.n_DIR_ALREADY_EXISTS]     = "The directory you wanted to create already exists.";
      application.xStatus[1][request.status.n_PLP_NO_NEXT_FUSE]       = "The system was unable to determine the next fuseaction that you should be heading to. You might want to inform the <a href=""mailto:?subject#request.dev.s_EMAIL#=request.status.n_PLP_NO_NEXT_FUSE"">website developer</a> about this problem.";
      application.xStatus[1][request.status.n_LOGIN_FAILED]           = "Failed to login.";
      application.xStatus[1][request.status.n_LOGOUT_SUCCESS]         = "You successfully logged out.";
      application.xStatus[1][request.status.n_ACCESS_DENIED]          = "Access to this section of the application is restricted";
      application.xStatus[1][request.status.n_INSERT_SECCESS]         = "Record inserted successfully.";
      application.xStatus[1][request.status.n_UPDATE_SECCESS]         = "Record updated successfully.";
      application.xStatus[1][request.status.n_DELETE_SECCESS]         = "Delete was executed successfully.";
    }
  </cfscript>
</cflock>

<!---
  If we have a status message store in the session scope, it will 'override'
  the attributes.nStatus - based status messages. This is to provide a way to
  show errors that have been caught in a <ctry> / <cfcatch> block.
--->
<cflock scope="session" timeout="2" type="exclusive">
  <cfif isDefined( "session.sStatus" )>
    <cfscript>
      /* 
       * Clear the value that has been set (so that it won't show up in every
       * page from now on. This means it will only be shown once.
       */
      request.sStatus = session.sStatus;
      structDelete( session, "sStatus" );
    </cfscript>

    <cfsetting enablecfoutputonly="No">
    <cfexit>
  </cfif>
</cflock>

<!--- 
  After the session has been checked/cleared, we go on with the 
  attributes.nStatus - based approach of status/error messaging.
--->
<cfif not isDefined( "attributes.nStatus" ) or ( attributes.nStatus eq 0 )>
  <cfsetting enablecfoutputonly="No">
  <cfexit>
</cfif>

<!--- Set the current --->
<cflock
  timeout="5"
  scope="Application"
  type="Readonly"
>
  <cfscript>
    if(( attributes.nStatus gt arrayLen( application.xStatus[1] )) or
       ( attributes.nStatus lt 0 ))
    {
      request.sStatus = "<b><u>Error</u></b>: " &
                        "[#request.udf.pathFormat( removeChars( lCase( getCurrentTemplatePath()), 1, len( request.site.s_ANCHOR_PATH )))#]" &
                        ": ArrayIndexOutOfRange [" & attributes.nStatus & " ";

      if( attributes.nStatus gt arrayLen( application.xStatus[1] ))
      {
        request.sStatus = request.sStatus & "&gt; " & arrayLen( application.xStatus[1] ) & "]";
      }
      else
      {
        request.sStatus = request.sStatus & "&lt; 0]";
      }
    }
    else
    {
      request.sStatus = application.xStatus[1][attributes.nStatus];
    }
  </cfscript>
</cflock>

<cfsetting enablecfoutputonly="No">